from datetime import datetime

import requests
from bs4 import BeautifulSoup


def banks_for_country(country_sign):
    cookies = {
        '_ga': 'GA1.1.479296914.1720401671',
        '_gcl_au': '1.1.1032536040.1720401688',
        '_clck': 'twbqhy%7C2%7Cfnc%7C0%7C1650',
        '__gads': 'ID=964377270d81dfdc:T=1720401670:RT=1720581300:S=ALNI_MafsA0w8NgPE7tBkjYnl9pmhj3pYA',
        '__gpi': 'UID=00000e8604216933:T=1720401670:RT=1720581300:S=ALNI_Mb53FjfZiuHahihoIjmI79dZukVCg',
        '__eoi': 'ID=1b11dd7476c0dc48:T=1720401670:RT=1720581300:S=AA-AfjZZvZ1USD1vHMtaccsoi2cV',
        'cfzs_google-analytics_v4': '%7B%22OFGW_pageviewCounter%22%3A%7B%22v%22%3A%2256%22%7D%7D',
        'cfz_google-analytics_v4': '%7B%22OFGW_engagementDuration%22%3A%7B%22v%22%3A%220%22%2C%22e%22%3A1752117309621'
                                   '%7D%2C%22OFGW_engagementStart%22%3A%7B%22v%22%3A%221720581309621%22%2C%22e%22'
                                   '%3A1752117309621%7D%2C%22OFGW_counter%22%3A%7B%22v%22%3A%22220%22%2C%22e%22'
                                   '%3A1752117309621%7D%2C%22OFGW_session_counter%22%3A%7B%22v%22%3A%2211%22%2C%22e'
                                   '%22%3A1752117309621%7D%2C%22OFGW_ga4%22%3A%7B%22v%22%3A%22fba09baf-bce5-4702-93ec'
                                   '-e6ba66292421%22%2C%22e%22%3A1752117309621%7D%2C%22OFGW__z_ga_audiences%22%3A%7B'
                                   '%22v%22%3A%22fba09baf-bce5-4702-93ec-e6ba66292421%22%2C%22e%22%3A1751937668785%7D'
                                   '%2C%22OFGW_let%22%3A%7B%22v%22%3A%221720581309621%22%2C%22e%22%3A1752117309621%7D'
                                   '%2C%22OFGW_ga4sid%22%3A%7B%22v%22%3A%221465424369%22%2C%22e%22%3A1720583109621%7D'
                                   '%7D',
        '_clsk': '188z711%7C1720581312147%7C4%7C1%7Cb.clarity.ms%2Fcollect',
        'FCCDCF': '%5Bnull%2Cnull%2Cnull%2Cnull%2Cnull%2Cnull%2C%5B%5B13%2C%22%5B%5C%22DBABBg~BUoAAACA%5C%22%2C%5B'
                  '%5B8%2C%5B1720581310%2C492527000%5D%5D%5D%5D%22%5D%5D%5D',
        'FCNEC': '%5B%5B%22AKsRol8-J5lWVCqrzW60kc_4Gp1u2URVgk-isLK'
                 '-8j8heZE7jDDa5M5LD60cOxxytuVQeV8nxzepDxreGS14GElagbkyeD55Z2LuGdpMxIQeC8YCZpVCTYZ5'
                 '-lS7uDgWJioZjN6b7WH4vRxBDdKfYb4-SzavqdphuQ%3D%3D%22%5D%5D',
        '_ga_CGDE2TMKTW': 'GS1.1.1720581300.8.1.1720581321.39.0.0',
        'XSRF-TOKEN': 'eyJpdiI6Im55YUZDRnBSU0hQdUIxL1BGazhYb2c9PSIsInZhbHVlIjoiMlBseGVLT0x3VWhvMHlZeW5zOW5vNHdBOHdGTTUvMlplVm0rMmM0QkcyQ2ZkS0dKM0hJd1NsZ1N5MHBMTGRNbFNUMlBnQ0hhTnFialIrY0Jtd1FyRGtYS1FpakErRDNSVk9MR2xoQ3dnczBDM0lVSWI5c1RlOCtiaGtKTXdEUTYiLCJtYWMiOiI1ODhmMjA3MWEwMWExNzM4MDNlZGM2NzQzZjNmOWFhMjA3MWM4ZmI4ZjEwMWExYjg2MjIwMjJhZjBhYzBkNjk4In0%3D',
        'bincheck_session': 'eyJpdiI6Imd0Sm9EaWFkU2Rtamk5U09BMVZSYkE9PSIsInZhbHVlIjoiOFgwNFRteGZmekxBYlBPcXRkcGo3RnlyNzkwWW5ienhXSjlCUlROcUxqa2V4azVrcFZNcmFVSWZIOUtwWjNOdTR6dUFxdEorN3FLQVczOFZZeEJ4d212SGFNSUF1ckpuS1lvSlE2dDlKejJaNm9HTFNybnVuQkMrMEdwVGFpeGYiLCJtYWMiOiIyZjNhZTZkNmViNzI1MGIyMDk5NjBkYzdlMDM4MTdhNmI1MjNhNzRlOWViZjk0MGUwYjg3OTU5OTRkNzJmZjQ1In0%3D',
    }

    headers = {
        'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,'
                  'application/signed-exchange;v=b3;q=0.7',
        'accept-language': 'en-US,en;q=0.9',
        'priority': 'u=0, i',
        'referer': 'https://bincheck.io/bin-list',
        'sec-ch-ua': '"Not/A)Brand";v="8", "Chromium";v="126", "Google Chrome";v="126"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"Windows"',
        'sec-fetch-dest': 'document',
        'sec-fetch-mode': 'navigate',
        'sec-fetch-site': 'same-origin',
        'sec-fetch-user': '?1',
        'upgrade-insecure-requests': '1',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                      'Chrome/126.0.0.0 Safari/537.36',
    }

    response = requests.get(f'https://bincheck.io/{country_sign}', cookies=cookies, headers=headers)
    if response.status_code == 200:
        return response.text
    else:
        print(response.status_code, country_sign, response.text)
        return None


def bank_html_analysis(html, countrys):
    """
    解析单个HTML,得到数据
    :param countrys:
    :param html:
    :return:
    """
    if html is None:
        print(countrys)
        return
    soup = BeautifulSoup(html, 'html.parser')
    title_items = (
        soup.find_all("div", class_="py-2 overflow-hidden")
    )
    result_list = []
    for title_item in title_items:
        banks = title_item.find("h3")
        contact = title_item.find_all("p")
        phone = ''
        web = ''
        if banks is None:
            continue
        if len(contact) > 0:
            phone = contact[0].text
            if "web" in phone:
                web = phone
                phone = ''
        if len(contact) > 1:
            web = contact[1].text
        json_item = {
            "bank": banks.text,
            "phone": phone,
            "website": web,
            "country_abbr": countrys,
            "create_time": datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
            "update_time": datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
        }
        result_list.append(tuple(json_item.values()))
    return result_list


def get_bank_total(countrySign):
    return bank_html_analysis(banks_for_country(countrySign), countrySign)


if __name__ == '__main__':
    result = get_bank_total("us")
    print(result)
